window.onload=function () {
    localStorage.removeItem('userHao');
    const container = document.getElementById('container');
    const signUpButton = document.getElementById('signUp');
    const signInButton = document.getElementById('signIn');
//点击注册
    signUpButton.onclick = function () {
        container.classList.add('penal-right-active');
    }
//点击登录
    signInButton.onclick = function () {
        container.classList.remove('penal-right-active');
    }


//获取验证码
    var btnVerify = document.getElementById('btnVerify');

    var email = document.getElementById('userEmail');
    btnVerify.onclick = function () {
        if (email.value === "") {
            alert("请输入邮箱地址");
        } else if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email.value)) {
            alert('请检查邮箱格式！')
        }
        $.ajax({
            url: "http://localhost:8080/future/user/getCode",
            method: "POST",
            data: {
                email: email.value,
            },
            success: function (data) {
                console.log(data);
                btnVerify.style.display = 'none';
                wait.style.display = 'block';
                cutSecond();
                if (data === true) {
                    alert("验证码发送成功，请稍等！")
                }
            },
            error: function (err) {//请求失败，执行的回调函数
                alert('错误');
                console.log('错误');


            }
        })
    }


//倒计时

    var wait = document.getElementsByClassName('wait')[0];
    var btnVerify = document.getElementById('btnVerify');
    var time = 60;
    var second = document.getElementsByClassName('second')[0];

    function cutSecond() {
        second.innerHTML = time;
        var aa = setInterval(function () {
            if (time == 0) {
                clearInterval(aa);
                btnVerify.style.display = 'inline';
                wait.style.display = 'none';
            } else {
                time -= 1;
                second.innerHTML = time;
            }
        }, 1000)
    }

// 注册
    var countNumber = document.getElementById('userHao');
    var userPassword = document.getElementById('userPwd');
    var code = document.getElementById('code');//验证码
    var register = document.getElementsByClassName('register')[0];
    var email = document.getElementById('userEmail');
    var Mail = document.getElementsByClassName('Mail')[0];
    //格式判断
    //账号判断
    function judgeHao() {
        if (countNumber.value.length === 0) {
            account.innerHTML = "账号不能为空";
        } else if (countNumber.value.length >= 8) {
            account.innerHTML = "账号超出可输入范围";
        } else {
            account.style.display = 'none';
        }
    }

    var account = document.getElementsByClassName('account')[0];
    countNumber.onclick = function () {
        account.style.display = 'block';
    }
    countNumber.onblur = judgeHao;

    //密码判断
    function judgeMi() {
        var numChar = new RegExp('^[0-9A-Za-z]+$');
        if (userPassword.value.length === 0) {
            cipher.innerHTML = "密码不能为空";
        } else if (userPassword.value.length < 8) {
            cipher.innerHTML = "密码太短";
        } else if (userPassword.value.length >= 12) {
            cipher.innerHTML = "密码超出可写入长度";
        } else if (!numChar.test(cipher.value)) {
            cipher.innerHTML = "密码由数字和字母组成";
        }else {
            cipher.style.display='none';
        }

    }

    var cipher = document.getElementsByClassName('cipher')[0];
    userPassword.onclick = function () {
        cipher.style.display = 'block';
    }
    userPassword.onblur = judgeMi;

    //邮箱判断
    function judgeYou() {
        Mail.style.display = 'block';
        if (email.value.length === 0) {
            Mail.innerHTML = "邮箱地址不能为空";
        } else if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email.value)) {
            Mail.innerHTML = "请检查邮箱格式";
        }else{
            Mail.style.display='none';
        }

    }

    email.onclick = function () {
        Mail.style.display = 'none';
    }
    email.onblur = judgeYou;

    //点击注册
    register.onclick = function () {
        $.ajax({
            url: "http://localhost:8080/future/user/register",
            method: "GET",
            data: {//参数
                userHao: countNumber.value,
                userPwd: userPassword.value,
                userEmail: email.value,
                code: code.value,
            },
            success: function (data) {
                console.log(data.statusCode);
                console.log(data.info);
                if (data.statusCode === 0) {//管理员
                    console.log(data.info);
                    alert(data.info);
                    location.reload();
                } else {
                    console.log(data.info);
                    alert(data.info);
                }

            },
            error: function (err) {//请求失败，执行的回调函数
                console.log('错误');
            }
        })
    }

    var third=document.getElementsByClassName('third')[0];

    third.onclick =function () {
        $.ajax({
            url: "http://localhost:8080/future/gitee",
            method: "GET",
            success: function (data) {
                localStorage.setItem("sign",1);
            },
            error: function (err) {//请求失败，执行的回调函数
                console.log('错误');
            }
        })
    }

    //登录
    var userHao = document.getElementById('Hao');
    var userPwd = document.getElementById('Pwd');
    var enter = document.getElementsByClassName('enter')[0];
    enter.onclick = function () {
        $.ajax({
            url: "http://localhost:8080/future/user/login",
            method: "POST",
            data: {//参数
                userHao: userHao.value,
                password: userPwd.value,
            },
            success: function (data) {
                console.log(data.info);
                if (data.statusCode === -1) {
                    alert('账号或密码输入错误！');
                    // swal("账号或密码输入错误");
                } else if (data.statusCode === 0) {
                    alert('登录成功！');
                    // swal("登录成功")
                    localStorage.setItem('userHao', userHao.value);//保存一下当前的用户名
                    window.location = data.info;
                } else {
                    // swal("登录成功")
                    alert('登录成功！');
                    localStorage.setItem('userHao', userHao.value);
                    window.location = data.info;
                }
            },
            error: function (err) {//请求失败，执行的回调函数
                console.log("错误");
            }

        })
    }


    // 忘记密码
    var forget = document.getElementsByClassName('forget')[0];
    var forgetMi_1 = document.getElementById('forgetMi_1');//第一个弹窗
    var forgetMi_2 = document.getElementById('forgetMi_2');//第二个弹窗
    var forgetMi_3 = document.getElementById('forgetMi_3');//第三个弹窗
    var close = document.getElementsByClassName('close');//关闭
    forget.onclick = function () {
        forgetMi_1.style.display = 'block';
    }
    for (var i = 0; i < close.length; i++) {
        close[i].onclick = function () {
            forgetMi_1.style.display = 'none';
            forgetMi_2.style.display = 'none';
            forgetMi_3.style.display = 'none';
        }
    }

    //第一个弹窗的下一步
    var btnNext_1 = document.getElementById('btnNext_1');
    var btnNext_2 = document.getElementById('btnNext_2');
    var btnNext_3 = document.getElementById('btnNext_3');
    var throughEmail = document.getElementsByClassName('throughEmail')[0];
    var throughEmail_2 = document.getElementsByClassName('throughEmail_2')[0];
    var throughEmail_3 = document.getElementsByClassName('throughEmail_3')[0];
    var userEmail = document.getElementsByClassName('userEmail')[0];
    var userEmail_2 = document.getElementsByClassName('userEmail_2')[0];
    btnNext_1.onclick = function () {
        console.log(throughEmail.value);
        nextFirst();
        if (throughEmail.value === "") {
            alert("请输入需要找回密码的账号!");
        } else {
            forgetMi_1.style.display = 'none';
            forgetMi_2.style.display = 'block';
        }
    }
    btnNext_2.onclick = function () {
        console.log(throughEmail.value);
        if ((throughEmail_2.value === " ") || (userEmail.value === "")) {
            alert("请输入邮箱及验证码!");
        } else {
            forgetMi_2.style.display = 'none';
            forgetMi_3.style.display = 'block';
        }
    }
    // btnNext_3.onclick = function () {
    //     console.log(throughEmail.value);
    //     if (throughEmail_3.value === userEmail_2.value) {
    //         forgetMi_1.style.display = 'none';
    //         forgetMi_2.style.display = 'none';
    //         forgetMi_3.style.display = 'none';
    //     } else if ((throughEmail_3.value === " ") || (userEmail_2.value === "")) {
    //         alert("新置密码不能为空！");
    //     } else {
    //         alert("两次输入密码不一样！");
    //     }
    // }
    var throughEmail = document.getElementsByClassName('throughEmail')[0];
    var btnNext_1 = document.getElementById('btnNext_1')[0];
   function nextFirst () {
        $.ajax({
            url: 'http://localhost:8080/future/user/lost-Hao',
            method: 'POST',
            data: {
                userHao: throughEmail.value,
            },
            success: function (data) {
                console.log(data);

            },
            error: function (err) {//请求失败，执行的回调函数
                console.log("错误");
            }

        })
    }

    //忘记密码获取验证码
    var getCode = document.getElementsByClassName('getCode')[0];
    var wait_3=document.getElementsByClassName('wait_3')[0];
    var userEmail = document.getElementsByClassName('userEmail')[0];
    var throughEmail_2=document.getElementsByClassName('throughEmail_2')[0]
    console.log(userEmail.value);
    getCode.onclick = function () {

        $.ajax({
            url: "http://localhost:8080/future/user/getCode",
            method: "POST",
            data: {
                email: throughEmail_2.value,
            },
            success: function (data) {

                console.log(data);
                getCode.style.display = 'none';
                wait_3.style.display = 'block';
                cutSecond_2();
                if (data === true) {
                    alert("验证码发送成功，请稍等！")
                }
            },
            error: function (err) {//请求失败，执行的回调函数
                alert('错误');
                console.log('错误');


            }
        })
    }
    //倒计时

    var time = 60;
    var second_2 = document.getElementsByClassName('second_2')[0];
    function cutSecond_2() {
        second_2.innerHTML = time;
        var aa = setInterval(function () {
            if (time == 0) {
                clearInterval(aa);
                getCode.style.display = 'inline';
                wait_3.style.display = 'none';
            } else {
                time -= 1;
                second_2.innerHTML = time;
            }
        }, 1000)
    }
    // 忘记密码第二步
var btnNext_2=document.getElementById('btnNext_2');
var userEmail=document.getElementsByClassName('userEmail')[0];
btnNext_2.onclick = function (){
    $.ajax({
        url:'http://localhost:8080/future/user/lost-code',
        method:'POST',
        data:{
            code:userEmail.value,
        },
        success: function (data) {

            if(data.statusCode===0){
                alert(data.info);
                forgetMi_2.style.display = 'none';
                forgetMi_3.style.display = 'block';
            }else{
                alert(data.info);
            }

        },
        error: function (err) {//请求失败，执行的回调函数
            alert('错误');
            console.log('错误');


        }
    })
}


    // 第三步
 var throughEmail_3=document.getElementsByClassName('throughEmail_3')[0];
 var userEmail_2=document.getElementsByClassName('userEmail_2')[0];

    btnNext_3.onclick=function (){
        $.ajax({
            url:'http://localhost:8080/future/user/checkPwd',
            method:'POST',
            data:{
                pwdone:throughEmail_3.value,
                pwdtwo:userEmail_2.value,
            },
            success: function (data) {
                console.log(data);
                alert(data.info);
                if(data.statusCode==1){
                    forgetMi_1.style.display = 'none';
                    forgetMi_2.style.display = 'none';
                    forgetMi_3.style.display = 'none';
                }
            },
            error: function (err) {//请求失败，执行的回调函数
                alert('错误');
                console.log('错误');


            }

        })
    }
}


